﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess.baobiaoDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.dataAcess.ViewDataSetTableAdapters;

namespace dhSystemBLL.orderData
{
  public  class pbomMasterBLL
    {

      private orderpnameTableAdapter _orderpnameAdapter = null;

      private orderpnameTableAdapter orderpnameAdapter
        {
            get
            {
                if (_orderpnameAdapter == null) _orderpnameAdapter = new orderpnameTableAdapter();
                return _orderpnameAdapter;
            }
        }
        pbomDetailModel pbomDetailRow = new pbomDetailModel();
        private V_pboTableAdapter _V_pboAdapter = null;

        private V_pboTableAdapter V_pboAdapter
        {
            get
            {
                if (_V_pboAdapter == null) _V_pboAdapter = new V_pboTableAdapter();
                return _V_pboAdapter;
            }
        }
        private V_pboMasterTableAdapter _V_pboMasterAdapter = null;

        private V_pboMasterTableAdapter V_pboMasterAdapter
        {
            get
            {
                if (_V_pboMasterAdapter == null) _V_pboMasterAdapter = new V_pboMasterTableAdapter();
                return _V_pboMasterAdapter;
            }
        }
      private pbomMasterTableAdapter _pbomMasterAdapter = null;

      private pbomMasterTableAdapter pbomMasterAdapter
        {
            get
            {
                if (_pbomMasterAdapter == null) _pbomMasterAdapter = new pbomMasterTableAdapter();
                return _pbomMasterAdapter;
            }
        }
        /// <summary>
      /// 查询产品BOM主表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
      public orderDataSet.pbomMasterDataTable getpbomMasterAll()
        {
            try
            {
                return pbomMasterAdapter.GetDatapbomMasterAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
      /// <summary>
      /// 查询pname视图
      /// </summary>
      /// <param name="row"></param>
      /// <returns></returns>
      public baobiaoDataSet.orderpnameDataTable getorderpnameAll()
      {
          try
          {
              return orderpnameAdapter.GetDataorderpnameALL ();
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message.ToString());
          }
      }
        /// <summary>
      /// 根据产品名称查询产品BOM主表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
      public orderDataSet.pbomMasterDataTable getpbomMasterAllBylikepname(string pname)
        {
            try
            {
                return pbomMasterAdapter.GetDatapbomMasterBylikepname (pname);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
      /// <summary>
      /// 根据产品名称查询产品BOM主表
      /// </summary>
      /// <param name="row"></param>
      /// <returns></returns>
      public orderDataSet.pbomMasterDataTable getpbomMasterAllBypname(string pname)
      {
          try
          {
              return pbomMasterAdapter.GetDatapbomMasterBypname1 (pname);
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message.ToString());
          }
      }
      /// 根据产品名称查询视图
      /// </summary>
      /// <param name="row"></param>
      /// <returns></returns>
      public ViewDataSet.V_pboMasterDataTable getV_pboMasterAllBypname(string pname)
      {
          try
          {
              return V_pboMasterAdapter.GetDataV_pboMasterBypname (pname);
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message.ToString());
          }
      }
      /// 根据产品名称查询视图
      /// </summary>
      /// <param name="row"></param>
      /// <returns></returns>
      public ViewDataSet.V_pboDataTable  getV_pboAllBypname(string pname)
      {
          try
          {
              return V_pboAdapter.GetDataV_pboBypname (pname);
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message.ToString());
          }
      }
        /// <summary>
      /// 插入产品BOM主表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
      public void  pbomMasterInsert(pbomMarkModel markrow,pbomMasterModel row,orderDataSet.pbomDetailDataTable pTable,string state,string pname)
        {
           
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                pbomDetailDAL pbomDetaildal = new pbomDetailDAL(tn, conn);
                pbomMasterDAL pbomMaster = new pbomMasterDAL(tn, conn);
                pbomMarkDAL pboMark = new pbomMarkDAL(tn, conn);
                if (state == "新增")
                {
                    
                    pbomMaster.pbomMasterInsert(row);
                    pboMark.pbomMarkInsert(markrow);
                    //更新单据编号的过程 
                }
                if (state == "编辑")
                {
                   
                    pbomMaster.pbomMasterUpdate(row);
                    pboMark.pbomMarkUpdate(markrow);
                }
                if (state == "覆盖")
                {

                    pbomMaster.pbomMasterUpdate(row);
                    pboMark.pbomMarkUpdate(markrow);
                    pbomDetaildal.pbomDetailDelALL(pname);

                }
                int i=1;
               
                foreach (orderDataSet.pbomDetailRow detailRow in pTable)
                {
                    if (detailRow.RowState != DataRowState.Deleted)
                    {
                        pbomDetailRow.Series = i;
                        pbomDetailRow.recordid = detailRow.recordid;
                        pbomDetailRow.pname = pname;
                         if (!detailRow.IswbNull ())
                        {
                        pbomDetailRow.wb = detailRow.wb;
                         }
                        else {pbomDetailRow.wb ="";}
                         if (!detailRow.IsmatNameNull ())
                         {
                             pbomDetailRow.matName  = detailRow.matName ;
                         }
                         else { pbomDetailRow.matName  = ""; }
                        if (!detailRow.IsptNull ())
                        {
                            pbomDetailRow.pt = detailRow.pt;
                        }
                        else { pbomDetailRow.pt = ""; }
                        if (!detailRow.IstsNull())
                        {
                            pbomDetailRow.ts = detailRow.ts;
                        }
                        else { pbomDetailRow.ts = 0; }
                        if (!detailRow.IsblenNull())
                        {
                            pbomDetailRow.blen = detailRow.blen;
                        }
                        else { pbomDetailRow.blen = 0; }
                        if (!detailRow.IsalenNull())
                        {
                            pbomDetailRow.alen = detailRow.alen;
                        }
                        else { pbomDetailRow.alen = 0; }
                        if (!detailRow.IsaweightNull())
                        {
                            pbomDetailRow.aweight = detailRow.aweight;
                        }
                            else {pbomDetailRow.aweight=0;}
                        if (!detailRow.IspweightNull ())
                        {
                            pbomDetailRow.pweight = detailRow.pweight;
                        }
                        else { pbomDetailRow.pweight  = 0; }
                        if (!detailRow.IspzNull())
                        {
                            pbomDetailRow.pz = detailRow.pz;
                        }
                        else { pbomDetailRow.pz = ""; }
                        if (!detailRow.IsmatNumNull())
                        {
                            pbomDetailRow.matNum = detailRow.matNum;
                        }
                        else { pbomDetailRow.matNum ="0"; }
                        if (!detailRow.IsmarkNull())
                        {
                            pbomDetailRow.mark = detailRow.mark;
                        }
                        else
                        {
                            pbomDetailRow.mark = "";
                        }
                        i++;
                    }
                   
                    //编辑子表记录,新增
                    if (detailRow.RowState == DataRowState.Added)
                    {
                       
                       pbomDetaildal .pbomDetailInsert (pbomDetailRow);

                    }
                    //修改
                    if (detailRow.RowState == DataRowState.Modified)
                    {
                       pbomDetaildal .pbomDetailUpdate (pbomDetailRow);
                    }
                    //删除
                    if (detailRow.RowState == DataRowState.Deleted)
                    {
                        DataRow delrow = (DataRow)detailRow;
                        pbomDetaildal.pbomDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                    }

                    
                }

                tn.Commit();
               

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个产品名称已经存在");
                    
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
      /// 更新产品BOM主表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
      public int pbomMasterUpdate(pbomMasterModel row, orderDataSet.pbomDetailDataTable pTable)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                pbomDetailDAL pbomDetaildal = new pbomDetailDAL(tn, conn);
                pbomMasterDAL pbomMaster = new pbomMasterDAL(tn, conn);
                pbomMaster.pbomMasterUpdate(row);
                foreach (orderDataSet.pbomDetailRow detailRow in pTable)
                {
                    pbomDetailRow.pname = detailRow.pname;
                    pbomDetailRow.wb = detailRow.wb;
                    pbomDetailRow.pt = detailRow.pt;
                    pbomDetailRow.ts = detailRow.ts;
                    pbomDetailRow.blen = detailRow.blen;
                    pbomDetailRow.alen = detailRow.alen;
                    pbomDetailRow.aweight = detailRow.aweight;
                    pbomDetailRow.pweight = detailRow.pweight;
                    pbomDetailRow.pz = detailRow.pz;
                    pbomDetailRow.matNum = detailRow.matNum;
                    pbomDetailRow.mark = detailRow.mark;
                   
                    //编辑子表记录,新增
                    if (detailRow.RowState == DataRowState.Added)
                    {

                        pbomDetaildal.pbomDetailInsert(pbomDetailRow);

                    }
                    //修改
                    if (detailRow.RowState == DataRowState.Modified)
                    {
                        pbomDetaildal.pbomDetailUpdate(pbomDetailRow);
                    }
                    //删除
                    if (detailRow.RowState == DataRowState.Deleted)
                    {
                        pbomDetaildal.pbomDetailDel(pbomDetailRow.matNum);
                    }
                }
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
               
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
      /// 删除一行产品BOM主表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
      public int pbomMasterDel(string pname)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                pbomMasterDAL pbomMaster = new pbomMasterDAL(tn, conn);
                pbomMaster.pbomMasterDel(pname);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
               
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
